﻿using System.Windows;
using RemoteTestRunner.BusinessLogic.ViewModels;
using RemoteTestRunner.BusinessLogic.Views;

namespace RemoteTestRunner.FormsUI.Wpf
{
    /// <summary>
    ///   Interaction logic for Admin.xaml
    /// </summary>
    public partial class ProjectAdmin : Window, IWpfDialogWindow
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectAdmin"/> class.
        /// </summary>
        public ProjectAdmin()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the SelectedItemChanged event of the tvwProjects control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="object"/> instance containing the event data.</param>
        private void tvwProjects_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var selectedProject = e.NewValue as ProjectViewModel;

            if (selectedProject != null)
            {
                var context = DataContext as ProjectAdminViewModel;

                context.SelectedProject = selectedProject;
            }

            var selectedTest = e.NewValue as TestViewModel;

            if (selectedTest != null)
            {
                var context = DataContext as ProjectAdminViewModel;

                context.SelectedTest = selectedTest;
            }

            btnDeleteProject.IsEnabled = selectedProject != null;
            btnDeleteTest.IsEnabled = selectedTest != null;
            btnAddTest.IsEnabled = selectedProject != null || selectedTest != null;
        }

        #endregion

        public void CloseDialog(bool? dialogResult)
        {
            DialogResult = dialogResult;
            Close();
        }
    }
}
